<?php
/**
 * 我的订单
 *
 *
 *
 *
 */

//use Shopnc\Tpl;

defined('InShopNC') or exit('Access Invalid!');

class member_orderControl extends mobileMemberControl {

	public function __construct(){
		parent::__construct();
	}
	
	/*
	 * 2017-01-17
	 * 团购订单列表
	 */
    public function groupbuy_order_listOp() {
        $model_order = Model('order');
        $condition = array();
        $condition['buyer_id'] = $this->member_info['member_id'];
        if ($_POST['order_sn']!=null) {
            $condition['order_sn'] = $_POST['order_sn'];
        }
    
        if ($_POST['order_state']!=null) {
            $condition['order_state'] = $_POST['order_state'];
        }
        if ($_POST['evaluation_state']!=null) {
            $condition['evaluation_state'] = $_POST['evaluation_state'];
        }
        //作为拼团的标记
        $condition['team_id']=array('gt',0);
        //$condition['order_state']=array('egt',20);
        $condition['order_state']=array(array('egt', 20),array('eq',0),'or');
    
        $limit = "";
        if (intval($_POST['page'])>0) {
            $limit =$this->page * (intval($_POST['page'])-1)." ".$this->page." ";
        }
        //拿到订单的主题信息
        $order_list_array = $model_order->getNormalOrderList($condition, 0, '*', 'order_id desc','', array('order_goods'));
    
        //把拼团数据加到$order_list_array里面
        //成员表                                                                   //团长表                                                               //用户信息
        $condition_mem['member_id']=$condition_team['lead_id']=$this->member_info['member_id'];
        $groupbuy_mem_model=Model('groupbuy_mem');
        $groupbuy_team_model=Model('groupbuy_team');
        foreach ($order_list_array as $key =>$val){
            //成员表                          //团长表                                                                                                //订单信息
            $condition_mem['team_id']=$condition_team['team_id']=$val['team_id'];
            $groupbuy_mem_info=$groupbuy_mem_model->groupbuy_mem_info($condition_mem);
            if (!empty($groupbuy_mem_info)){
                //找到参团表，再找开团表
                if ($groupbuy_mem_info['lead_flag']==1){
                    $groupbuy_mem_info['content']='开团';
                    $team_info=$groupbuy_team_model->groupbuy_team_info($condition_team);
                    $groupbuy_mem_info['team_number']=$team_info['team_number'];
                    $groupbuy_mem_info['record_num']=$team_info['record_num'];
    
                    if ($groupbuy_mem_info['team_number'] == $groupbuy_mem_info['record_num']){
                        $groupbuy_mem_info['groupbuy_status']='1';//成功
                    }else if($groupbuy_mem_info['team_number'] > $groupbuy_mem_info['record_num'] && TIMESTAMP < $team_info['end_time']){
                        $groupbuy_mem_info['groupbuy_status']='2';//进行中
                    }else if($groupbuy_mem_info['team_number'] > $groupbuy_mem_info['record_num'] && TIMESTAMP > $team_info['end_time']){
                        $groupbuy_mem_info['groupbuy_status']='0';//失败
                    }
                }else{
                    $groupbuy_mem_info['content']='参团';
                    //参团也需要这些数据
                    unset($condition_team['lead_id']);
    
                    $team_info=$groupbuy_team_model->groupbuy_team_info($condition_team);
                    $groupbuy_mem_info['team_number']=$team_info['team_number'];
                    $groupbuy_mem_info['record_num']=$team_info['record_num'];
                    if ($groupbuy_mem_info['team_number'] == $groupbuy_mem_info['record_num']){
                        $groupbuy_mem_info['groupbuy_status']='1';
                    }else if($groupbuy_mem_info['team_number'] > $groupbuy_mem_info['record_num'] && TIMESTAMP < $team_info['end_time']){
                        $groupbuy_mem_info['groupbuy_status']='2';
                    }else if($groupbuy_mem_info['team_number'] > $groupbuy_mem_info['record_num'] && TIMESTAMP > $team_info['end_time']){
                        $groupbuy_mem_info['groupbuy_status']='0';
                    }
    
                }
    
                $order_list_array[$key]['groupbuy_mem_info']=$groupbuy_mem_info;
                //查找发货时间
                //                 $order_common_info=$model_order->getOrderCommonInfo($order_list_array[$key]['order_id']);
                //                 $order_list_array[$key]['shipping_time']=$order_common_info['shipping_time'];
    
    
            }
            if($groupbuy_mem_info['pay_status']!=1){
                unset($order_list_array[$key]);
            }
    
        }
    
        $order_group_list = array();
        $order_pay_sn_array = array();
        foreach ($order_list_array as $value) {
    
            //显示取消订单
            $value['if_cancel'] = $model_order->getOrderOperateState('buyer_cancel',$value);
            //显示收货
            $value['if_receive'] = $model_order->getOrderOperateState('receive',$value);
            //显示锁定中
            $value['if_lock'] = $model_order->getOrderOperateState('lock',$value);
            //显示物流跟踪
            $value['if_deliver'] = $model_order->getOrderOperateState('deliver',$value);
            //商品图
            foreach ($value['extend_order_goods'] as $k => $goods_info) {
                $value['extend_order_goods'][$k]['goods_image_url'] = cthumb($goods_info['goods_image'], 240, $value['store_id']);
            }
            $order_group_list[$value['pay_sn']]['order_list'][] = $value;
            //如果有在线支付且未付款的订单则显示合并付款链接
            if ($value['order_state'] == ORDER_STATE_NEW) {
                $order_group_list[$value['pay_sn']]['pay_amount'] += $value['order_amount'] - $value['rcb_amount'] - $value['pd_amount'];
            }
            $order_group_list[$value['pay_sn']]['add_time'] = $value['add_time'];
            //记录一下pay_sn，后面需要查询支付单表
            $order_pay_sn_array[] = $value['pay_sn'];
        }
    
        $new_order_group_list = array();
        foreach ($order_group_list as $key => $value) {
            $value['pay_sn'] = strval($key);
            $new_order_group_list[] = $value;
        }
    
        $page_count = $model_order->gettotalpage();
    
        $array_data = array('order_group_list' => $new_order_group_list);
        if(isset($_GET['getpayment'])&&$_GET['getpayment']=="true"){
            $model_mb_payment = Model('mb_payment');
    
            $payment_list = $model_mb_payment->getMbPaymentOpenList();
            $payment_array = array();
            if(!empty($payment_list)) {
                foreach ($payment_list as $value) {
                    $payment_array[] = array('payment_code' => $value['payment_code'],'payment_name' =>$value['payment_name']);
                }
            }
            $array_data['payment_list'] = $payment_array;
        }
          
        //
        //output_data(array('order_group_list' => $array_data), mobile_page($page_count));
        output_data($array_data, mobile_page($page_count));
    }

    /**
     * 订单列表
     */
    public function order_listOp() {
		$model_order = Model('order');
        $condition = array();
        $condition['buyer_id'] = $this->member_info['member_id'];
        if ($_POST['order_sn']!=null) {
            $condition['order_sn'] = $_POST['order_sn'];
        }
        
        if ($_POST['order_state']!=null) {
            $condition['order_state'] = $_POST['order_state'];
        } 
        if ($_POST['evaluation_state']!=null) {
            $condition['evaluation_state'] = $_POST['evaluation_state'];
        } 
        //进行团购判断
        $condition['team_id']=0;

        
        $limit = "";
        if (intval($_POST['page'])>0) {
            $limit =$this->page * (intval($_POST['page'])-1)." ".$this->page." ";
        }
        $order_list_array = $model_order->getNormalOrderList($condition, 0, '*', 'order_id desc','', array('order_goods'));
        $order_group_list = array();
        $order_pay_sn_array = array();
        foreach ($order_list_array as $value) {
            //显示取消订单
            $value['if_cancel'] = $model_order->getOrderOperateState('buyer_cancel',$value);
            //显示收货
            $value['if_receive'] = $model_order->getOrderOperateState('receive',$value);
            //显示锁定中
            $value['if_lock'] = $model_order->getOrderOperateState('lock',$value);
            //显示物流跟踪
            $value['if_deliver'] = $model_order->getOrderOperateState('deliver',$value);
            //商品图
            foreach ($value['extend_order_goods'] as $k => $goods_info) {
                $value['extend_order_goods'][$k]['goods_image_url'] = cthumb($goods_info['goods_image'], 240, $value['store_id']);
            }
            $order_group_list[$value['pay_sn']]['order_list'][] = $value;
            //如果有在线支付且未付款的订单则显示合并付款链接
            if ($value['order_state'] == ORDER_STATE_NEW) {
                $order_group_list[$value['pay_sn']]['pay_amount'] += $value['order_amount'] - $value['rcb_amount'] - $value['pd_amount'];
            }
            $order_group_list[$value['pay_sn']]['add_time'] = $value['add_time'];
            //记录一下pay_sn，后面需要查询支付单表
            $order_pay_sn_array[] = $value['pay_sn'];
        }
        
        $new_order_group_list = array();
        foreach ($order_group_list as $key => $value) {
            $value['pay_sn'] = strval($key);
            $new_order_group_list[] = $value;
        }

        $page_count = $model_order->gettotalpage();

        $array_data = array('order_group_list' => $new_order_group_list);
        if(isset($_GET['getpayment'])&&$_GET['getpayment']=="true"){
            $model_mb_payment = Model('mb_payment');

            $payment_list = $model_mb_payment->getMbPaymentOpenList();
            $payment_array = array();
            if(!empty($payment_list)) {
                foreach ($payment_list as $value) {
                    $payment_array[] = array('payment_code' => $value['payment_code'],'payment_name' =>$value['payment_name']);
                }
            }
            $array_data['payment_list'] = $payment_array;
        }

        //output_data(array('order_group_list' => $array_data), mobile_page($page_count));
        output_data($array_data, mobile_page($page_count));
    }

    
    
    /**
     * 2017-01-18
     * 如果未付款
     * 删除该用户的开团订单
     */
    public function delete_team_orderOp(){
        $condition_mem['member_id']=$condition_team['lead_id']=$condition_or['buyer_id']=$this->member_info['member_id'];
        $condition_or['team_id']=array('gt',0);
        $condition_or['order_state']=array('lt',20);
        //判断team_id不等于0
        //$condition_or['team_id']=array('lt',20);
        $order_model=Model('order');
        $order_list=$order_model->getOrderList($condition_or);

        //得到的都是拼团的
        //返回的结果有一串的
        if (!empty($order_list)){
            $groupbuy_team_model=Model('groupbuy_team');
            $groupbuy_mem_model=Model('groupbuy_mem');
            foreach ($order_list as $key =>$val){
                if($val['order_state'] < 20){
                    //删除订单
                    /* echo '<pre>';
                    var_export($val);die; */
                    $result=$this->team_order_delete($val['order_id']);
                    if ($result){
                        //删除拼团
                        $condition_team['team_id']=$val['team_id'];
                        //检查是否团长
                        $result_lead=$groupbuy_team_model->check_team($condition_team);
                        if ($result_lead){
                            $result_team=$groupbuy_team_model->delete_team($condition_team);
                            if($result_team){
                                //删除成员表
                                $condition_mem['team_id']=$val['team_id'];
                                $result_mem=$groupbuy_mem_model->delete_mem($condition_mem);
                                if ($result_mem){
                                    output_data(array('status'=>'success','msg'=>'取消拼团成功'));
                                }else{
                                    output_data(array('status'=>'error','msg'=>'系统繁忙mem'));
                                }
                            
                            }else{
                                output_data(array('status'=>'error','msg'=>'系统繁忙team'));
                            } 
                        }else{
                            //参团
                            $condition_mem['team_id']=$val['team_id'];
                            $result_mem=$groupbuy_mem_model->delete_mem($condition_mem);
                            if ($result_mem){
                                output_data(array('status'=>'success','msg'=>'取消拼团成功'));
                            }else{
                                output_data(array('status'=>'error','msg'=>'系统繁忙mem'));
                            }
                        }
                     
                        
                    }else{
                        output_data(array('status'=>'error','msg'=>'系统繁忙order'));
                    }
                }
                
            }
            
            
        }else{
            output_data(array('status'=>'error','msg'=>'订单未支付'));
        }
    }
    

    /**
     * 
     * 删除未付款的拼团订单操作
     *这个时候类似于取消拼团
     */
    private function team_order_delete($order_id) {
        //$state_type = 'drop';
        $model_order = Model('order');
        $logic_order = Logic('order');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['buyer_id'] = $this->member_info['member_id'];
        $order_info = $model_order->getOrderInfo($condition);
        /* echo '<pre>';
        var_export($order_info);die; */
        /* $if_allow = $model_order->getOrderOperateState('drop',$order_info);
        echo $if_allow;die;
        if (!$if_allow) {
            return false;
        } */
        $result = $logic_order->changeOrderStateCancel($order_info,'buyer', $this->member_info['member_name'], '其它原因');
        //$result = $logic_order->changeOrderStateRecycle($order_info,'buyer',$state_type);
        return $result['state'];
        /* if(!$result['state']) {
            output_error($result['msg']);
        } else {
            output_data(array('msg'=>'操作成功！'));
        } */
    }
    
    /**
     * 取消订单
     */
    public function order_cancelOp() {
        $model_order = Model('order');
        $logic_order = Logic('order');
        $order_id = intval($_POST['order_id']);

        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['buyer_id'] = $this->member_info['member_id'];
        $order_info	= $model_order->getOrderInfo($condition);
        $if_allow = $model_order->getOrderOperateState('buyer_cancel',$order_info);
        if (!$if_allow) {
            output_error('无权操作');
        }

        $result = $logic_order->changeOrderStateCancel($order_info,'buyer', $this->member_info['member_name'], '其它原因');
        if(!$result['state']) {
            output_error($result['msg']);
        } else {
            output_data(array('msg'=>'操作成功！'));
        }
    }

    /**
     * 订单确认收货
     * 可用
     */
    public function order_receiveOp() {
        $model_order = Model('order');
        $logic_order = Logic('order');
        $order_id = intval($_POST['order_id']);
        
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['buyer_id'] = $this->member_info['member_id'];
        $order_info	= $model_order->getOrderInfo($condition);
        $if_allow = $model_order->getOrderOperateState('receive',$order_info);
        if (!$if_allow) {
            output_error(array('status'=>'error','msg'=>'无权操作'));
        }

        $result = $logic_order->changeOrderStateReceive($order_info,'buyer', $this->member_info['member_name']);
        if(!$result['state']) {
            output_error(array('status'=>'error','msg'=>$result['msg']));
        } else {
            output_data(array('status'=>'success','msg'=>'操作成功！'));
        }
    }
    
    /**
     * 2017-02-10
     * 拼团订单交易完成，删除订单操作
     */
    public function drop_team_orderOp(){
        //订单交易完成之后删除，商品数不会增加
        $state_type = 'delete';
        $order_id   = intval($_REQUEST['order_id']);
        $model_order = Model('order');
        $logic_order = Logic('order');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['buyer_id'] = $this->member_info['member_id'];
        $order_info = $model_order->getOrderInfo($condition);
        /* $if_allow = $model_order->getOrderOperateState('delete',$order_info);
        if (!$if_allow) {
            output_error('无权操作');
        } */
        $result = $logic_order->changeOrderStateRecycle($order_info,'buyer',$state_type);
        if(!$result['state']) {
            output_error(array('status'=>'error','msg'=>$result['msg']));
        } else {
            output_data(array('status'=>'success','msg'=>'操作成功！'));
        }
        
    }

    /**
     * 删除订单操作
     *
     */
    public function order_deleteOp() {
        $state_type = 'delete';   
        $order_id   = intval($_POST['order_id']);
        $model_order = Model('order');
        $logic_order = Logic('order');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['buyer_id'] = $this->member_info['member_id'];
        $order_info = $model_order->getOrderInfo($condition);
        $if_allow = $model_order->getOrderOperateState('delete',$order_info);
        if (!$if_allow) {
            output_error(array('status'=>'error','msg'=>'无权操作'));
        }
        $result = $logic_order->changeOrderStateRecycle($order_info,'buyer',$state_type);
        if(!$result['state']) {
            output_error(array('status'=>'error','msg'=>$result['msg']));
        } else {
            output_data(array('status'=>'success','msg'=>'操作成功'));
        }
    }


    /**
     * 物流跟踪
     */
    public function search_deliverOp(){
        $order_id	= intval($_POST['order_id']);
        if ($order_id <= 0) {
            output_error('订单不存在');
        }

        $model_order	= Model('order');
        $condition['order_id'] = $order_id;
        $condition['buyer_id'] = $this->member_info['member_id'];
        $order_info = $model_order->getOrderInfo($condition,array('order_common','order_goods'));
        if (empty($order_info) || !in_array($order_info['order_state'],array(ORDER_STATE_SEND,ORDER_STATE_SUCCESS))) {
            output_error('订单不存在');
        }

        $express = rkcache('express',true);
        $e_code = $express[$order_info['extend_order_common']['shipping_express_id']]['e_code'];
        $e_name = $express[$order_info['extend_order_common']['shipping_express_id']]['e_name'];

        //$deliver_info = $this->_get_express($e_code, $order_info['shipping_code']);
        output_data(array('express_name' => $e_name,'express_code' => $e_code, 'shipping_code' => $order_info['shipping_code']));
        //output_data(array('express_name' => $e_name, 'shipping_code' => $order_info['shipping_code'], 'deliver_info' => $deliver_info));
    }

    /**
     * 从第三方取快递信息
     *
     */
    public function _get_express($e_code, $shipping_code){

        $url = BASE_SITE_URL.'http://www.kuaidi100.com/query?type='.$e_code.'&postid='.$shipping_code.'&id=1&valicode=&temp='.random(4).'&sessionid=&tmp='.random(4);
        import('function.ftp');
        $content = dfsockopen($url);
        $content = json_decode($content,true);

        if ($content['status'] != 200) { 
            output_error('物流信息查询失败');
        }
        $content['data'] = array_reverse($content['data']);
        $output = array();
        if (is_array($content['data'])){
            foreach ($content['data'] as $k=>$v) {
                if ($v['time'] == '') continue;
                $output[]= $v['time'].'&nbsp;&nbsp;'.$v['context'];
            }
        }
        if (empty($output)) exit(json_encode(false));
        if (strtoupper(CHARSET) == 'GBK'){
            $output = Language::getUTF8($output);//网站GBK使用编码时,转换为UTF-8,防止json输出汉字问题
        }

        return $output;
    }

}